package com.tristia.resources.impl;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tristia.dao.GenericDao;
import com.tristia.entity.Cd;
import com.tristia.entity.Tracks;

@Component
@Scope("request")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_ATOM_XML })
public class TracksResWs {
	@Resource
	GenericDao<Cd, Long> cdDao;
	@Context 
	UriInfo ui;
	@Context
	Request request;
	private String id; 
	private Logger log = Logger.getLogger(this.getClass());
	
	public TracksResWs(){
	}
	
	public TracksResWs(GenericDao<Cd, Long> cdDao, UriInfo ui, Request request) {
		this.cdDao = cdDao;
        this.ui = ui;
        this.request = request;
    }
	
	public TracksResWs(GenericDao<Cd, Long> cdDao, UriInfo ui, Request request, String id) {
		this.cdDao = cdDao;
        this.ui = ui;
        this.request = request;
        this.id = id;  
    }
	
	@GET
	public Tracks getTracks() {
		log.info("getTracks entered");
		Tracks tracks = new Tracks();
		log.info("Tracks num: " + cdDao.get(Long.parseLong(id)).getTracks().size());
		tracks.setTracks(cdDao.get(Long.parseLong(id)).getTracks());
		log.info("Sending: \n\n" + tracks.toString() + "\n\n");
		return tracks;
	}
	
	@Path("/{trackId}")
	public TrackResWs getTrackResWs(@PathParam("trackId") int trackId) {
		log.info("getTrack entered");
		return new TrackResWs(cdDao, ui, request, id, trackId);
	}
}
